<template>
  <div class="login-container">
      {{loginForm}}
    <el-form ref="loginForm" :model="loginForm" class="login-form" auto-complete="on" label-position="left" label-width="80px">
      <h3 class="title">尚品汇商城系统</h3>
      <el-form-item prop="userName" label="用户名">
        <el-input v-model="loginForm.userName" name="userName" type="text" auto-complete="on" placeholder="用户名" />
      </el-form-item>
      <el-form-item prop="password" label="密码">
        <el-input
          type="password"
          v-model="loginForm.password"
          name="password"
          auto-complete="on"
          placeholder="密码"/>
      </el-form-item>
      <el-form-item prop="code" label="验证码">
          <el-col :span="16">
            <el-input
            v-model="loginForm.code"
            auto-complete="off"
            placeholder="验证码"
            style="width: 63%"
            >
            </el-input>
        </el-col>
        <el-col :span="8">
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img"/>
        </div>
        </el-col>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" style="width:100%;" @click="handleLogin">
          登录
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import authApi from '../../api/auth.js'
export default {
  name: 'Login',
  data() {
    return {
      codeUrl:'',
      loginForm: {
        userName: 'admin',
        password: '123456',
        code:'',
        uuid:''
      }
    }
  },
  methods: {
    getCode(){
        authApi.genCaptcha().then(res=>{
            console.log(res.data.data.imageBase64)
            this.codeUrl = res.data.data.imageBase64;
            this.loginForm.uuid = res.data.data.uuid;
        })
    },
    handleLogin() {
      authApi.login(this.loginForm).then(res=>{
          if(res.data.ok){
              
              let token = res.data.data.token;
              let userId = res.data.data.userId;
              sessionStorage.setItem("token",token)
              sessionStorage.setItem("userId",userId)
              alert(res.data.data.message)
              this.$router.push({path:"/spu"})
          }
      })
    },
  },
  mounted(){
      this.getCode();
  }
}
</script>

<style>


</style>
